
//获取元素
let mobile = document.querySelector('input[type="text"]');
let pwd = document.querySelector('input[type="password"]');
let btn = document.querySelector('input[type="submit"]');
let sendText = document.querySelector('.sendText');
let sendDom = document.querySelector('.send');
let titleDom = document.querySelector('.title'); //账号密码错误提示信息
//定时器初始值
let time = 5;
let timer;
let flag1 = flag2 = false;
//手机号是否符合要求
mobile.onblur = function () {

    if (/^1[356789]\d{9}$/.test(mobile.value)) {

        this.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        flag1 = true;
    } else {
        this.nextElementSibling.innerHTML = `<span style = "color:red ;font-size:12px;">格式有误</span>`;
        flag1 = false;
    }
    changeRe()
}
//密码是否符合要求
pwd.onblur = function () {
    if (pwd.value.length >= 6) {
        this.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        flag2 = true;
    } else {
        this.nextElementSibling.innerHTML = `<span style = "color:red ;font-size:12px;">格式有误</span>`;
        flag2 = false;
    }
    changeRe()
}


//各项数据符合要求，解除禁用
function changeRe() {
    (flag1 && flag2) ? (btn.disabled = false) : (btn.disabled = true);
}


// 登录
btn.onclick = () => {
    // 发送ajax请求获取数据
    axios.post(
        'http://39.98.112.34:8000/base/user/login',
        { mobile: mobile.value, password: pwd.value }
    ).then((res) => {
        let response = res.data;
        // console.log(res.data.data)
        if (response.code != 200) {
            titleDom.innerHTML = '账号或密码错误！！！'
            return;
        }
         // encodeURIComponent 编码
         setCookie('userInfo', encodeURIComponent(JSON.stringify(response.data)))
        
        //  console.log(getCookie('userInfo'));
        // 获取要跳转的地址
        // getUrlParam从URL地址上 获取到要跳转的地址，如果取到了就直接URL解码，否则 就默认的index.html
        let callback = getUrlParam('callback') ? decodeURIComponent(getUrlParam('callback')):'index.html';
        location.href = callback;
        // console.log(callback)

        

       
        

    })

}

